<?php
/**
 * 基控制器
 */

class BaseAction extends Action {
    public function __construct() {
        parent::__construct();

        if (!logined()) redirect(U('Login/index'));

        $this->user = $_SESSION['user'];
        $this->allows = array_merge(
            array(
                'Index' => '主页',
                'Personal' => '个人'),
            allows($this->user['role_id']));

        if (!array_key_exists(MODULE_NAME, $this->allows))
            $this->error('你不具备访问该模块的权限');

        $this->navs = navsGenerate($this->allows);
        $this->breadcrumbs = array(
            array(
                'name' => '主页',
                'url' => U('Index/index'),
            ),
        );
    }

    protected function breadcrumbsPush($name, $url) {
        $breadcrumbs = $this->breadcrumbs;
        $breadcrumbs[] = array(
            'name' => $name,
            'url' => $url);
        $this->breadcrumbs = $breadcrumbs;
    }

    public function index() {
        $this->display('Common/subnav');
    }

    /** 数据模型数据项删除 */
    function model_delete($model_name) {
        $model = D($model_name);
    }

    function crud($model_name) {
        $model = D($model_name);
        switch ($this->_param('o')) {
            case 'add':
                if ($model->create()) {
                    if ($model->add()) {
                        $this->alert = array(
                            'type' => 'success',
                            'info' => '添加成功',
                        );
                    } else {
                        $this->alert = array(
                            'type' => 'error',
                            'info' => '添加失败',
                        );
                    }
                } else {
                    $this->alert = array(
                        'type' => 'error',
                        'info' => $model->getError()
                    );
                }
                break;

            case 'update':
                if ($model->create()) {
                    if ($model->save()) {
                        $this->alert = array(
                            'type' => 'success',
                            'info' => '修改成功',
                        );
                    } else {
                        $this->alert = array(
                            'type' => 'error',
                            'info' => '修改失败',
                        );
                    }
                } else {
                    $this->alert = array(
                        'type' => 'error',
                        'info' => $model->getError()
                    );
                }
                break;

            case 'delete':
                if (!$model->where(array(
                    'id' => $_POST['id']))->delete()) {
                    $this->alert = array(
                        'type' => 'error',
                        'info' => '删除失败'
                    );
                } else {
                    $this->alert = array(
                        'type' => 'success',
                        'info' => '删除成功',
                    );
                }
                break;
        }
    }
}
